package com.regal.packet;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.xmlpull.v1.XmlPullParser;

/**
 *
 */
public class GroupChat extends IQ {

	private final List<GroupItem> gItems = new ArrayList<GroupItem>();
	private String operate;
	private String jid;
	private String name;

    /**
     * Element name of the packet extension.
     */
    public static final String ELEMENT_NAME = "query";

    /**
     * Namespace of the packet extension.
     */
    public static final String NAMESPACE = "jabber:iq:groupchat";
    
    public void addGroupItem(GroupItem item) {    	
    	synchronized (gItems) {
    		gItems.add(item);
        }    	
    }
    
    public int getGroupItemCount() {
        synchronized (gItems) {
            return gItems.size();
        }
    }
    
    public Collection<GroupItem> getGroupItems() {
        synchronized (gItems) {
            return Collections.unmodifiableList(new ArrayList<GroupItem>(gItems));
        }
    }

    public String getChildElementXML() {
        StringBuffer buf = new StringBuffer();
        buf.append("<query xmlns=\"" + NAMESPACE + "\">");
        if(getType() == IQ.Type.SET){
        	buf.append(" <conference name=\""+getName()+"\"");
        	buf.append(" operate=\""+getOperate()+"\"  jid=\""+getJid()+"\"");
        	buf.append("/>");   
        }	 
        buf.append("</query>");
        return buf.toString();
    }
    
    /**
     * A Group item, which consists of a name ,jid...
     */
    public static class GroupItem {

    	private String name;
    	private String jid; 
    	        
    	public GroupItem() {
        
        }
        /**
         * Creates a new group item.
         *
         * @param user the name.
         * @param name the jid.
         */
        public GroupItem(String name, String jid) {
            this.name = name;
            this.jid = jid;
        }
		public String getJid() {
			return jid;
		}
		public void setJid(String jid) {
			this.jid = jid;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		

    }
    /**
     * An IQProvider for GroupListIQ packet.
     *
     * @author Derek DeMoro
     */
    public static class Provider implements IQProvider {

        public Provider() {
            super();
        }

        public IQ parseIQ(XmlPullParser parser) throws Exception {
            
        	GroupChat gChat = new GroupChat();
        	GroupItem gItem = null;
        	System.out.println("enter parse IQ of GroupChat");

            boolean done = false;
            while (!done) {
                int eventType = parser.next();
                if (eventType == XmlPullParser.START_TAG) {
                	gItem = new GroupItem();
                    if (parser.getName().equals("item")) {
                    	//String name = parser.getAttributeValue("", "name");
                    	String name = parser.getAttributeValue("", "natualname");
                    	gItem.setName(name);
                    	String jid = parser.getAttributeValue("", "jid");
                    	gItem.setJid(jid);
                    }                    
                }
                else if (eventType == XmlPullParser.END_TAG) {
                	if (parser.getName().equals("item")) {
                		gChat.addGroupItem(gItem);
                		System.out.println("add GItem"+gItem.getName()+gItem.getJid());
                    }
                    if (parser.getName().equals(ELEMENT_NAME)) {
                        done = true;
                    }
                }
            }
            return gChat;
        }
    }
	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getJid() {
		return jid;
	}

	public void setJid(String jid) {
		this.jid = jid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
